data_panel$wave <- data_panel$wave + 1
data_cross$wave <- data_cross$wave + 1

#save dvs of interest
dvs <- c('personal_dobbs', 'personal_roe', 'courtlegit_composite',  
         'courtsize', 'courtterms', 'opinion', 'moral_opinion', 
         'norm', 'moral_norm', 'fetal_scale', 'wave', 'pid', "weight", "weight_ps")


##### gather dataset so all variables are in variable column 
sum_panel <- data_panel %>% select(dvs)  %>%
  gather(variable, value, -c(wave, pid, weight_ps), factor_key=TRUE)

sum_cross <- data_cross %>% select(dvs)  %>%
  gather(variable, value, -c(wave, pid, weight), factor_key=TRUE)

#summarize the data

sum_panel_ideo <- ddply(sum_panel, c("wave", "variable", "pid"), summarise,
                        N    = sum(!is.na(value)),
                        mean = weighted.mean(value, weight_ps,na.rm=TRUE),
                        sd   = sd(value, na.rm=TRUE),
                        se   = weighted_se(value, weight_ps, na.rm=T),
                        max = max(value, na.rm=TRUE),
                        min = min(value, na.rm=TRUE))

sum_panel_ideo_full <- ddply(sum_panel, c("wave", "variable"), summarise,
                             N    = sum(!is.na(value)),
                             mean = weighted.mean(value, weight_ps, na.rm=TRUE),
                             sd   = sd(value, na.rm=TRUE),
                             se   = weighted_se(value, weight_ps, na.rm=T),
                             max = max(value, na.rm=TRUE),
                             min = min(value, na.rm=TRUE))
sum_panel_ideo_full$pid <- 'Total'
sum_panel <- rbind(sum_panel_ideo, sum_panel_ideo_full)

sum_cross_ideo <- ddply(sum_cross, c("wave", "variable", "pid"), summarise,
                        N    = sum(!is.na(value)),
                        mean = weighted.mean(value, weight, na.rm=TRUE),
                        sd   = sd(value, na.rm=TRUE),
                        se   = weighted_se(value, weight, na.rm=T),
                        max = max(value, na.rm=TRUE),
                        min = min(value, na.rm=TRUE))

sum_cross_ideo_full <- ddply(sum_cross, c("wave", "variable"), summarise,
                             N    = sum(!is.na(value)),
                             mean = weighted.mean(value, weight, na.rm=TRUE),
                             sd   = sd(value, na.rm=TRUE),
                             se   = weighted_se(value, weight, na.rm=T),
                             max = max(value, na.rm=TRUE),
                             min = min(value, na.rm=TRUE))
sum_cross_ideo_full$pid <- 'Total'
sum_cross <- rbind(sum_cross_ideo, sum_cross_ideo_full)



